<template>
  <!-- 购物车列表 -->
  <div class="cart">
    <h4>全部商品 ({{ cartInfoList.length }})</h4>
    <div class="cart-main">
      <!-- 标题栏 -->
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <!-- 商品主体 -->
      <div class="cart-body">
        <!-- 商品列表 -->
        <ul class="cart-list" v-for="(cart, index) in cartInfoList" v-bind:key="cart.id">
          <li class="cart-list-con1">
            <input type="checkbox" name="chk_list" v-bind:checked="cart.isChecked"
                   v-on:click="changeChecked(cart, $event)">
          </li>
          <li class="cart-list-con2">
            <img v-bind:src="cart.imgUrl" alt="规定图像的替代文本" title="显示点什么哒">
            <div class="item-msg">
              {{ cart.skuName }}
            </div>
          </li>
          <li class="cart-list-con3">
            <div class="item-txt">
              语音升级款
            </div>
          </li>
          <li class="cart-list-con4">
            <span class="price">
              {{ cart.skuPrice }}.00
            </span>
          </li>
          <li class="cart-list-con5">
            <a href="javascript:void(0)" class="mins" v-on:click="subSkuNum(cart)">-</a>
            <input type="number" name="" id="" v-bind:value="cart.skuNum" max="999" min="1" autocomplete="off"
                   class="itxt" v-on:input="updSkuNum(cart, $event)">
            <a href="javascript:void(0)" class="plus" v-on:click="addSkuNum(cart)">+</a>
          </li>
          <li class="cart-list-con6">
            <span class="sum">
              {{ cart.skuNum * cart.skuPrice }}.00
            </span>
          </li>
          <li class="cart-list-con7">
            <a href="javascript:void(0);" class="sindelet" v-on:click="deleteCartById(cart)">删除</a>
            <br>
            <a href="javascript:void(0);">移到收藏</a>
          </li>
        </ul>
      </div>
    </div>
    <!-- 商品操作 -->
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" v-bind:checked="isCartChecked && cartInfoList.length > 0"
               v-on:change="updateAllChecked($event)">
        <span>全选</span>
      </div>
      <div class="option">
        <a href="javascript:void(0);" v-on:click="deleteAllCart($event)">删除选中的商品</a>
        <a href="javascript:void(0);">移到我的关注</a>
        <a href="javascript:void(0);">清除下柜商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">已选择<span>{{ cartInfoList.length }}</span>件商品</div>
        <div class="sumprice">
          <em>总价(不含运费):&nbsp;</em>
          <i class="summoney">{{ totalPrice }}.00</i>
        </div>
        <div class="sumbtn">
          <!-- <a class="sum-btn" href="javascript:void(0);" target="_self" v-on:click="$router.push('trade')">去结算</a> -->
          <router-link class="sum-btn" to="/trade">去结算</router-link>
        </div>
      </div>
    </div>

    <div class="cart-like">
      <ul class="react-tabs__tab-list" role="tablist">
        <li class="br-tab-title br-tab-title-curr" role="tab" id="react-tabs-0" aria-selected="true"
            aria-disabled="false" aria-controls="react-tabs-1" tabindex="0"></li>
        <li class="br-tab-title" role="tab" id="react-tabs-2" aria-selected="false" aria-disabled="false"
            aria-controls="react-tabs-3"></li>
      </ul>
    </div>
  </div>
</template>

<script>
// 状态管理（Vuex）四大集合函数
import {mapGetters} from "vuex";
// 按需引入lodash节流函数
import throttle from "lodash/throttle";
// 按需引入lodash防抖函数
import debounce from "lodash/debounce";

export default {
  name: 'CartView',
  computed: {
    ...mapGetters(["cartInfo"]),
    cartInfoList() {
      return this.cartInfo.cartInfoList || [];
    },
    totalPrice() {
      return this.cartInfoList.reduce((a, b) => a + b.skuPrice * b.skuNum, 0);
    },
    isCartChecked() {
      return (this.cartInfoList.filter((item) => item.isChecked === "1").length === this.cartInfoList.length);
    }
  },
  methods: {
    // 初始化数据函数
    initialData() {
      this.$store.dispatch("getShopCart");
    },
    // 节流函数：递增商品数量+1
    addSkuNum: throttle(async function (cart) {
      const params = {skuId: cart.skuId, skuNum: +1};
      try {
        await this.$store.dispatch("addOrUpdateCart", params);
        this.initialData();
      } catch (error) {
        alert(error.message);
      }
    }, 2000),
    // 节流函数：递减商品数量-1
    subSkuNum: throttle(async function (cart) {
      if (cart.skuNum > 1) {
        let params = {skuId: cart.skuId, skuNum: -1};
        try {
          await this.$store.dispatch("addOrUpdateCart", params);
          this.initialData();
        } catch (error) {
          alert(error.message);
        }
      }
    }, 2000),
    // 防抖函数：输入商品数量+n/-n
    updSkuNum: debounce(async function (cart, event) {
      let params = {skuId: cart.skuId};
      let userResultValue = event.target.value * 1;
      if (isNaN(userResultValue) || userResultValue < 1) {
        params.skuNum = 0;
      } else {
        params.skuNum = parseInt(userResultValue) - cart.skuNum;
      }
      try {
        await this.$store.dispatch("addOrUpdateCart", params);
        this.initialData();
      } catch (error) {
        alert(error.message);
      }
    }, 500),
    // 根据SkuId删除
    async deleteCartById(cart) {
      let skuId = cart.skuId;
      try {
        await this.$store.dispatch("deleteCartById", skuId);
        this.initialData();
      } catch (error) {
        alert(error.message);
      }
    },
    // 修改某一个商品的勾选的状态
    async changeChecked(cart, event) {
      let params = {skuId: cart.skuId, isChecked: event.target.checked ? "1" : "0"};
      try {
        await this.$store.dispatch("changeChecked", params);
        this.initialData();
      } catch (error) {
        alert(error.message);
      }
    },
    // 全选的业务
    async updateAllChecked(event) {
      let isChecked = event.target.checked ? "1" : "0";
      try {
        await this.$store.dispatch("allUpdateChecked", isChecked);
        this.initialData();
      } catch (error) {
        alert(error.message);
      }
    },
    // 删除选中的商品
    async deleteAllCart() {
      try {
        await this.$store.dispatch("deleteAllCart");
        this.initialData();
      } catch (error) {
        alert(error.message);
      }
    }
  },
  mounted() {
    // 通过钩子函数Mounted初始化数据函数
    this.initialData();
  }
}
</script>

<style lang="less" scoped>
.cart {
  width: 1200px;
  margin: 0 auto;

  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }

  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;

      & > div {
        float: left;
      }

      .cart-th1 {
        width: 25%;

        input {
          vertical-align: middle;
        }

        span {
          vertical-align: middle;
        }
      }

      .cart-th2 {
        width: 25%;
      }

      .cart-th3, .cart-th4, .cart-th5, .cart-th6 {
        width: 12.5%;

      }
    }

    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;

      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;

        & > li {
          float: left;
        }

        .cart-list-con1 {
          width: 4.1667%;
        }

        .cart-list-con2 {
          width: 25%;

          img {
            width: 82px;
            height: 82px;
            float: left;
          }

          .item-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }

        .cart-list-con3 {
          width: 20.8333%;

          .item-txt {
            text-align: center;
          }
        }

        .cart-list-con4 {
          width: 12.5%;

        }

        .cart-list-con5 {
          width: 12.5%;

          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }

          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 31px;
            float: left;
            text-align: center;
            font-size: 14px;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }
        }

        .cart-list-con6 {
          width: 12.5%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 12.5%;

          a {
            color: #666;
          }
        }
      }
    }
  }

  .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;
      }
    }

    .money-box {
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 16px;
        }
      }

      .sumbtn {
        float: right;

        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: "Microsoft YaHei";
          background: #e1251b;
          overflow: hidden;
        }
      }
    }
  }

  .cart-like {
    width: 990px;
    margin: 0 auto;

    .br-tab-wrap {
      margin-top: 28px;

      .br-tab-title-curr {
        color: #e2231a;
        font-weight: 700;
        border-color: #e2231a;
      }
    }
  }
}
</style>
